package interview.Datastructures;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Node{
	int data;
	Node next;	
}
class LinkedList{
	public static Node add(int num,Node first){
		Node newNode = new Node();
		newNode.data=num;
		newNode.next=null;	
		if (first!=null){
			Node temp=first;
			while(temp.next!=null) temp=temp.next;
			temp.next=newNode;			
		}
		else
			first=newNode;
		return first;
	}
	
	public static Node delete(int num,Node first){
		if(first==null) return first;
		else{
			if (first.data==num) return first.next;
			else{
				Node shadow=first;
				while((shadow!=null)&&(shadow.next!=null)&&(shadow.next.data!=num)){
					shadow=shadow.next;					
				}
				if ((shadow.next!=null)&&(shadow.next.data==num)) {
					shadow.next=shadow.next.next;					
				}
				return first;
			}
		}
	}
	public static void printList(Node start){
		while(start!=null){
			System.out.print(start.data+"  --->  ");
			start=start.next;
		}
	}
}

class Input{
	public static int getIntInput(){
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        
		int i=0;
		try {
			System.out.print("Enter Integer:");
			i = Integer.parseInt(br.readLine());
		} catch (IOException e) {
			e.printStackTrace();
		}
        catch(NumberFormatException nfe){
            System.err.println("Invalid Format!");
        }
        return i;
	}
}
public class SingleLinkedListTest {	
	
	public static void main(String[] args) {
		Node start = null;
		for(int i=1;i<=10;i++){
			int num = Input.getIntInput();			
			start=LinkedList.add(num, start);		
		}
		LinkedList.printList(start);
		
		System.out.println("\n\nEnter number to delete:");
		int num= Input.getIntInput();
		start=LinkedList.delete(num, start);
		LinkedList.printList(start);
		
		
		System.out.println("\n\nEnter number to delete:");
		num= Input.getIntInput();
		start=LinkedList.delete(num, start);
		LinkedList.printList(start);
		
		
		System.out.println("\n\nEnter number to delete:");
		num= Input.getIntInput();
		start=LinkedList.delete(num, start);
		LinkedList.printList(start);
		
		System.out.println("\n\nEnter number to delete:");
		num= Input.getIntInput();
		start=LinkedList.delete(num, start);
		LinkedList.printList(start);
		
	}
	
	

}
